package org.shagin.jfin.ui.date.cond;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.jdesktop.swingx.JXDatePicker;
import org.shagin.jfin.core.Debug;
import org.shagin.jfin.date.cond.ExactDate;
import org.shagin.jfin.date.cond.IDateCondition;
import org.shagin.jfin.ui.UI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The Class JExactDatePane.
 */
@SuppressWarnings("serial")
public class JExactDatePane extends DateConditionPane<ExactDate> {

    private static final Logger log = LoggerFactory
            .getLogger(JExactDatePane.class);

    private JXDatePicker datePicker = new JXDatePicker();

    /**
     * Instantiates a new j exact date pane.
     */
    public JExactDatePane() {
        super();
        dateCondition = new ExactDate();
        setLayout(new FlowLayout(FlowLayout.CENTER, UI.HGAP, UI.VGAP));
        datePicker.setDate(getValue().getDate());
        add(datePicker);
        initEvents();
    }

    /**
     * Inits the events.
     */
    private void initEvents() {

        datePicker.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Debug.traceEnter(log);
                getValue().setDate(datePicker.getDate());
                fireChangedEvent();
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setDateCondition(IDateCondition dateCondition) {
        super.setDateCondition(dateCondition);
        datePicker.setDate(getValue().getDate());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ExactDate getValue() {
        return (ExactDate) dateCondition;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getTitle() {
        // TODO Localize
        return "В дату";
    }

}
